$().ready(function(e) {
    signin.init();
});
var signin = {
	init: function(){
		$('#doLogin').submit(function(){
			$('button.doLoginBtn').attr('disabled',true);
			msg = '';
			msg += '<div class="alert alert-info">';
				msg += '<i class="glyphicon glyphicon-search"></i> Authenticating...';
			msg += '</div>';
			$('.msgHolder').html(msg);
			setTimeout(function(){
				signin.doSignIn();
			},3000);
		});
	},
	doSignIn: function(){
		var strUserName = $('#strUserName').val();
		var strPassword = $('#strPassword').val();
		var strRememberMe = $('input#rememberMe[type="checkbox"]').is(':checked');
		uri = 'login/do_login';
		data = {
			'strUserName' : strUserName,
			'strPassword' : strPassword,
			'strRememberMe' : strRememberMe
		};
		$.ajax({
			type: "POST",
			url: base_url+uri,
			data: data,
			dataType: 'json',
			success: function(result){
				msg = '';
				if(result.status == 'put_username'){
					msg += '<div class="alert alert-warning">';
						msg += '<i class="glyphicon glyphicon-exclamation-sign"></i> Please put your username.';
					msg += '</div>';
				} else if(result.status == 'put_password') {
					msg += '<div class="alert alert-warning">';
						msg += '<i class="glyphicon glyphicon-exclamation-sign"></i> Please put your password.';
					msg += '</div>';
				} else if(result.status == 'username_not_exist') {
					msg += '<div class="alert alert-warning">';
						msg += '<i class="glyphicon glyphicon-exclamation-sign"></i> Username is not exist.';
					msg += '</div>';
				} else if(result.status == 'email_not_exist') {
					msg += '<div class="alert alert-warning">';
						msg += '<i class="glyphicon glyphicon-exclamation-sign"></i> E-mail is not exist.';
					msg += '</div>';
				} else if(result.status == 'user_deleted') {
					msg += '<div class="alert alert-warning">';
						msg += '<i class="glyphicon glyphicon-exclamation-sign"></i> User is not exist/deleted.';
					msg += '</div>';
				} else if(result.status == 'password_mismatch') {
					msg += '<div class="alert alert-warning">';
						msg += '<i class="glyphicon glyphicon-exclamation-sign"></i> Your password is incorrect.';
					msg += '</div>';
				} else {
					msg += '<div class="alert alert-success">';
						msg += '<i class="glyphicon glyphicon-ok-sign"></i> You are successfully logged-in. Redirecting';
					msg += '</div>';
					setTimeout(function(){
						window.location = base_url+'profile/'+result.username;
					},3000);
				}
				$('.msgHolder').html(msg);
				$('button.doLoginBtn').attr('disabled',false);
				setTimeout(function(){
					$('.alert').fadeOut('slow');	
				},3000);
			}
		});
	}
}